#include<bits/stdc++.h>
using namespace std;
int n;
int p[201];
int f[204][204][204];
int main() {
	freopen("shung.in","r",stdin);
	freopen("shung.out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1; i<=n; i++)
		cin>>p[i];
	for(int i=1; i<=n; i++) {
		for(int j=0; j<=n+1; j++) {
			for(int k=0; k<=n+1; k++) {
				f[i][max(j,p[i])][k]=max(f[i][max(j,p[i])][k],f[i-1][j][k]+(j<=p[i]));
				f[i][j][min(p[i],k)]=max(f[i][j][min(p[i],k)],f[i-1][j][k]+(k>=p[i]));
			}
		}
	}
	int ans=0;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			ans=max(ans,f[n][i][j]);
	cout<<ans;
	return 0;
}



